import { Component } from "@angular/core";
import { AdvertisementBean } from "./bean/advertisement.bean";
import { HttpServcie } from "../../service/http.service";
import {
  NzModalService,
  NzModalSubject,
  NzNotificationService
} from "ng-zorro-antd";
import { Config } from "./../../service/config";

@Component({
  selector: "advertisement-page",
  templateUrl: "advertisement.html",
  styleUrls: ["advertisement.scss"]
})
export class Advertisement {
  modelTitle: string;
  //表格
  config: any = Config;
  updateData: any = {};
  data: Array<any> = [];
  currentModal: NzModalSubject;
  modal: any = 0;
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  advertisementTypes = Config.ADVERTISEMENT_TYPE;
  bean:AdvertisementBean=new AdvertisementBean();

  constructor(
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService
  ) {
    this.query();
  }


  //查询
  query() {
    this.httpServcie.advertiseQueryAll().subscribe(data => {
      if (data.status == "200") {
        if (data.data.list) {
          this.data = data.data.list;
        } else {
          this.data = [];
        }
      } else {
        this._notification.create("error", "错误提示", data.message);
      }
    });
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.data.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.data.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }

  //全选
  checkAll(value) {
    if (value) {
      this.data.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.data.forEach(data => (data.checked = false));
    }
  }

  //添加
  add(title, content, footer) {
    this.modelTitle = "添加";
    this.modal = 0;
    //  if(this.data && this.data.length>0){
    //    this._notification.create('error', '错误提示','请删除后再添加!');
    //    return;
    //  }
    this.currentModal = this.modalService.open({
      title:title,
      width: 600,
      content: content,
      closable: false,
      showConfirmLoading: true
    });
  }
  //
  update(titleTpl, contentTpl, footerTpl) {
      this.modelTitle = "修改";
      this.modal = 1;
      const updateList: any = [];

      this.data.forEach(element=>{
        if (element.checked) {
          updateList.push(element);
          this.updateData = element;
        }
      });
    if (updateList.length > 1) {
      // 异常
      this._notification.create("warning", "警告提示", "只能选中一条数据！");
      return;
    }
    if (updateList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }
    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 600,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    })
  }



  delete() {
    const deleteList: any = [];
    this.data.forEach(element => {
      if (element.checked) {
        deleteList.push(element.id + "");
      }
    });

    // if (deleteList.length > 1) {
    //   // 异常
    //   this._notification.create("error", "错误提示", "只能选一条数据！");
    //   return;
    // }
    if (deleteList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选一条数据！");
      return;
    }
    let thiss = this;
    this.modalService.confirm({
      title: "您是否确认要删除这项内容",
      content: "<b>一旦删除，无法还原</b>",
      onOk() {
        thiss.httpServcie.advertiseDelete({ ids: deleteList}).subscribe(
          data => {
            if (data.status == "200") {
              thiss.query();
            } else {
              this._notification.create("error", "错误提示", data.message);
            }
          },
          error => {
            this._notification.create("error", "错误提示", "删除失败！");
          }
        );
      },
      onCancel() {}
    });
  }

  //添加返回
  addCall(event) {
    this.currentModal.destroy();
    this.query();
  }

  //查看大图
  previewImage: string = ""; //图片地址
  previewVisible: boolean = false; //是否显示
  showImage(param) {
    this.previewImage =
      this.config.IP + this.config.API.getUpload + "/" + param;
    this.previewVisible = true;
  }

  /**
   * 返回一个集合
   * @param para
   * @returns {string[]}
   */
  showImageList(para) {
    if (para) {
      return para.split(",");
    }
    return [];
  }

  openDocs(url) {
    let list: Array<string> = [
      "doc",
      "dot",
      "docx",
      "dotx",
      "docm",
      "dotm",
      "xls",
      "xlt",
      "xla",
      "xlsx",
      "xltx",
      "xlsm",
      "xltm",
      "xlam",
      "xlsb",
      "ppt",
      "pot",
      "pps",
      "ppa",
      "pptx",
      "potx",
      "ppsx",
      "ppam",
      "pptm",
      "potm",
      "ppsm"
    ];
    let lookUrl = "";
    let tt = url.split(".");
    let houzui = null;
    if (tt.length > 1) {
      houzui = tt[tt.length - 1];
    }
    if (houzui) {
      if (list.indexOf(houzui) >= 0) {
        lookUrl = Config.OFFICE_URL + Config.PREVIEW_URL + url;
      } else {
        lookUrl = Config.PREVIEW_URL + url;
      }
    } else {
      lookUrl = Config.PREVIEW_URL + url;
    }
    // window.open(lookUrl);
    let openBean = window.open() ;
    openBean.location.replace(lookUrl);

  }
}
